package com.itbaizhan;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/*读取web.xml中的信息*/
/*获取web.xml文件中的信息

1<context-param>
2    <param-name>key</param-name>
3    <param-value>value</param-value>
4</context-param>


servletContext.getInitParameter("key")
该方法可以读取web.xml文件中标签中的配置信息。(知道key时运用)

servletContext.getInitParameterNames()
该方法可以读取web.xml文件中所有param-name标签中的值。
（key与value都不知道时就用这获取所有的key值）*/
public class ContextReadInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取servletContent对象
        ServletContext servletContext = this.getServletContext();
        //获取所有的<param-name>的值
        Enumeration<String> initParameterNames =servletContext.getInitParameterNames();
        //输出响应
        PrintWriter pw = resp.getWriter();

        while (initParameterNames.hasMoreElements()){
            //取到param-name 值
            String name = initParameterNames.nextElement();
            //获取param-value
            String value = servletContext.getInitParameter(name);

            pw.println("name:"+name+"=value:"+value);
        }
        pw.flush();
        pw.close();
    }
}
